package com.ybkj.daijia.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverEnchashment;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverEnchashmentService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.serverUtils.ExcelUtil;
import com.ybkj.daijia.serverUtils.SettingUtils;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "driverenchashment")
@Slf4j
public class DriverEnchashmentController {


    @Autowired
    private DriverService driverService;
    @Autowired
    private DriverEnchashmentService driverEnchashmentService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    /**
     * 提现申请四个统计数据
     * @return
     */
    @RequestMapping("statistics")
    @ResponseBody
    public String statistics(){
        Subject subject = SecurityUtils.getSubject();
        JSONObject json = new JSONObject();
        if(subject.isPermitted("driverenchashment")){
            json = driverEnchashmentService.statistics();
        }
        return json.toJSONString();
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(String param) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("driverenchashment")) {
            return "driverenchashment/list";
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public Page<DriverEnchashment> listData(String param) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("driverenchashment")) {
            Page<DriverEnchashment> queryAll = driverEnchashmentService.queryByEmploy(null, param, Pager.getPage(), Pager.getLimit());
            return queryAll;
        } else {
            throw new NoPermissionException();
        }

    }

    /****
     * 审核
     * @param id
     * @param result
     * @return
     */
    @RequestMapping(value = "audit", method = RequestMethod.POST)
    public synchronized @ResponseBody
    Result<DriverEnchashment> audit(
        @RequestParam("id") Long id,
        @RequestParam("result") String result) {
        Result<DriverEnchashment> vo = new Result<DriverEnchashment>();

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("driverenchashment")) {
            DriverEnchashment driverEnchashment = driverEnchashmentService.findOne(id);
            if (!driverEnchashment.getStatus().equals(DriverEnchashment.STATUS_WAIT)) {
                vo.setMessage("该记录已审核通过或驳回，不能重复操作");
                return vo;
            }

            Driver driver = driverService.findOne(driverEnchashment.getEmployId());

            driverEnchashmentService.audit(driverEnchashment, driver, result);

            String detail = "【%s】审核了服务人员【" + driver.getUsername() + "】的提现申请";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            vo.setSuccess(true);
            return vo;
        } else {
            vo.setSuccess(false);
            vo.setMessage("您没有该权限");
            return vo;
        }
    }


    @RequestMapping(value = "exportMoney", method = RequestMethod.GET)
    public @ResponseBody
    String exportOrder(
        @RequestParam(value = "param") String param,
        HttpServletRequest req, HttpServletResponse response, HttpSession session
    ) throws IOException, ParseException {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        List<DriverEnchashment> content = new ArrayList<DriverEnchashment>();
        if (principal.isCompanyType()) {
            content = driverEnchashmentService.listByEmploy(null, param);
        } else {
            content = driverEnchashmentService.listByEmploy(null, param);
        }
        List<List<String>> rowlist = new ArrayList<List<String>>();
        String[] head = new String[27];
        head[0] = ("状态");
        head[1] = ("姓名");
        head[2] = ("工号");
        head[3] = ("手机号");
        head[4] = ("申请金额");
        head[5] = ("申请时间");
        head[6] = ("所属银行");
        head[7] = ("卡号");
        head[8] = ("收款人姓名");

        for (DriverEnchashment de : content) {
            List<String> celllist = new ArrayList<String>();
            if (de.getStatus() == 0) {
                celllist.add("待审核");
            } else if (de.getStatus() == 1) {
                celllist.add("已同意");
            } else if (de.getStatus() == 2) {
                celllist.add("已驳回");
            }
            celllist.add(de.getEmployRealName());
            celllist.add(de.getEmployUserName());
            celllist.add(de.getEmployPhone());
            celllist.add(de.getMoney().toString());
            SimpleDateFormat sf = new SimpleDateFormat("yy"
                + "yy-MM-dd HH:mm");
            String date = sf.format(de.getCreated());
            celllist.add(date);
            celllist.add(de.getBank());
            celllist.add(de.getCardNumber());
            celllist.add(de.getMasterName());

            rowlist.add(celllist);
        }

        return ExcelUtil.createExcel(req, response, head, rowlist);
    }


    @RequestMapping(value = "exportMoneyProcess", method = RequestMethod.GET)
    public @ResponseBody
    Object exportOrderProcess(HttpSession session) {
        return session.getAttribute("exportZhuancheProcess");
    }


    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}

